package com.sxfq.loansupermarket.backsystem.modular.sharesHolding.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.entity.QualityAssuranceInfo;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.entity.SearchQualityAssuranceInfo;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.service.IDentalImageService;
import com.sxfq.loansupermarket.backsystem.modular.sharesHolding.entity.SharesAreCompanyInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 质量管理控制器
 *
 * @author fengshuonan
 * @Date 2019-09-01 15:34:05
 */
@Controller
@RequestMapping("/sharesHolding")
public class SharesHoldingController extends BaseController {
    @Autowired
    IDentalImageService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;


    private String PREFIX = "/sharesHolding/";

    /**
     * 跳转到企业谱图
     */
    @RequestMapping("sharesContentAtlas")
    public String sharesContentAtlas(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        //对外投资
        String sqlo="select a.id,a.company_name from share_sare_company_info a where a.company_type=1 ";
        List<SharesAreCompanyInfo> outerList=sqlMapper.selectList(sqlo,SharesAreCompanyInfo.class);
        //分支机构
        String sqlB="select a.id,a.company_name from share_sare_company_info a where a.company_type=2 ";
        List<SharesAreCompanyInfo> branchList=sqlMapper.selectList(sqlB,SharesAreCompanyInfo.class);
        model.addAttribute("outerList",outerList);
        model.addAttribute("outerListCount",outerList.size());
        model.addAttribute("branchList",branchList);
        model.addAttribute("branchListCount",branchList.size());
        return PREFIX + "sharesHolding_atlas.html";
    }

    /**
     * 跳转到股份查询
     */
    @RequestMapping("sharesContentQuery")
    public String sharesContentQuery(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String sql="select b.account,c.company_name,a.subscription_amount,c.money,FORMAT(a.subscription_amount/c.money,2) as moneyProportion " +
                "from share_sare_relation_info a " +
                "left join user b on (a.userid=b.id) " +
                "left join share_sare_company_info c on (a.share_sare_company_id=c.id) " +
                "where a.userid='"+ShiroKit.getUser().getId()+"'";
        List<SharesAreCompanyInfo> infolist=sqlMapper.selectList(sql,SharesAreCompanyInfo.class);
        String sql2="select b.account,count(1) as count,sum(a.subscription_amount),sum(c.money) as sumMoney,FORMAT(sum(a.subscription_amount)/sum(c.money),2) as moneyProportion " +
                "from share_sare_relation_info a left join user b on (a.userid=b.id) " +
                "left join share_sare_company_info c on (a.share_sare_company_id=c.id) " +
                "where a.userid='"+ShiroKit.getUser().getId()+"'";
        SharesAreCompanyInfo info2=sqlMapper.selectOne(sql2,SharesAreCompanyInfo.class);
        model.addAttribute("infolist",infolist);
        model.addAttribute("info2",info2);
        return PREFIX + "sharesHolding_query.html";
    }
}
